package com.zhentao.product.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.login.service.TUserService;
import com.zhentao.nlz.project.dto.TDeptDto;
import com.zhentao.nlz.project.login.TUser;
import com.zhentao.nlz.project.product.TDept;
import com.zhentao.nlz.project.product.TTenant;
import com.zhentao.product.mapper.TDeptMapper;
import com.zhentao.product.mapper.TTenantMapper;
import com.zhentao.product.service.TDeptService;
import com.zhentao.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author 不.凌
* @description 针对表【t_dept(部门表)】的数据库操作Service实现
* @createDate 2024-09-22 21:09:18
*/
@Service
public class TDeptServiceImpl extends ServiceImpl<TDeptMapper, TDept>
    implements TDeptService{
    @Autowired
    private TDeptMapper deptMapper;
    @Autowired
    private TTenantMapper tenantMapper;
    @Autowired
    private TUserService tUserService;
    @Override
    public Result deptByParentId(TDeptDto deptDto) {
        TUser user = tUserService.getUsers();
        System.err.println("用户信息111"+user);
            List<TDept> list = this.list(Wrappers.<TDept>lambdaQuery()
                    .eq(TDept::getParentId, 0)
                    .eq(user.getTenantId()!=0,TDept::getTenantId, user.getTenantId()));

//        遍历所有的
        for (TDept tDept : list) {
            TTenant tTenant = tenantMapper.selectOne(Wrappers.<TTenant>lambdaQuery()
                    .eq(tDept.getTenantId() != null, TTenant::getId, tDept.getTenantId()));
            tDept.setTenantName(tTenant.getName());
            //        根据父类id查询它线面的所有子分类数据
            List<TDept> children = this.list(Wrappers.<TDept>lambdaQuery()
                    .eq(TDept::getParentId, tDept.getDeptId()));
            for (TDept child : children) {
                TTenant tTenant1 = tenantMapper.selectOne(Wrappers.<TTenant>lambdaQuery()
                        .eq(child.getTenantId() != null, TTenant::getId, child.getTenantId()));
                tDept.setTenantName(tTenant1.getName());
            }
            tDept.setChildren(children);
        }
        return Result.success(list);
    }
//    第二版--回家在研究
//    @Override
//    public Result deptByParentId(TDeptDto deptDto) {
////        先根据查询所有的租户表
//        List<TTenant> tenantList = tenantMapper.list();
////        根据租户表查询他所属的一系列父类部门
//        for (TTenant tTenant : tenantList) {
//            List<TDept> list = this.list(Wrappers.<TDept>lambdaQuery()
//                    .eq(tTenant.getId() != null, TDept::getTenantId, tTenant.getId())
//                    .eq(TDept::getParentId, 0));
////            在通过租户所有的父级部门去查询所有的子级部门
//            for (TDept tDept : list) {
//                //        根据父类id查询它线面的所有子分类数据
//                List<TDept> children = this.list(Wrappers.<TDept>lambdaQuery()
//                        .eq(TDept::getParentId, tDept.getDeptId()));
////                把子级部门放到父级部门里
//                tDept.setChildren(children);
//            }
//            //        现在父级部门也有子级部门 将所有的部门给所属的租户
//            tTenant.setDeptList(list);
//        }
//        return Result.success(tenantList);
//    }
    @Override
    public Result deptChildrenById(Integer parentId) {
//        根据父类id查询它线面的所有子分类数据
        List<TDept> list = this.list(Wrappers.<TDept>lambdaQuery()
                .eq(TDept::getParentId, parentId));
        return Result.success(list);
    }

    @Override
    public Result deptById(Integer id) {
//        根据id查询数据
        TDept list = this.getOne(Wrappers.<TDept>lambdaQuery()
                .eq(TDept::getDeptId, id));
            TTenant tTenant = tenantMapper.selectOne(Wrappers.<TTenant>lambdaQuery()
                    .eq(list.getTenantId() != null, TTenant::getId, list.getTenantId()));
        list.setTenantName(tTenant.getName());

        return Result.success(list);
    }

    @Override
    public Result deptUpdate(TDept dept) {
        dept.setUpdateTime(new DateTime());
        boolean b = this.updateById(dept);
        return b?Result.success("成功"):Result.error("失败");

    }

    @Override
    public Result deptDeleteByDeptId(TDept dept) {
        boolean b = this.removeById(dept);
        return b?Result.success("成功"):Result.error("失败");
    }

    @Override
    public Result deptSave(TDept dept) {
        dept.setCreateTime(new DateTime());
        boolean b = this.save(dept);
        return b?Result.success("成功"):Result.error("失败");
    }

    @Override
    @Transactional
    public Result deptDelAll(TDept dept) {
        System.out.println(dept);
        List<TDept> list = this.list(Wrappers.<TDept>lambdaQuery()
                .eq(dept.getDeptId() != null, TDept::getParentId, dept.getDeptId()));
        if (list.size()>0){
            for (TDept tDept : list) {
                this.removeById(tDept);
            }
        }
        boolean b = this.removeById(dept);
        return b?Result.success("成功"):Result.error("失败");
    }

    @Override
    public Result deptByTenantId(Integer tenantId) {
        List<TDept> list = this.list(Wrappers.<TDept>lambdaQuery()
                .eq(TDept::getTenantId, tenantId)
                .eq(TDept::getParentId, 0));
        return Result.success(list);
    }

    @Override
    public List<TDept> deptListByTenantId(Integer tenantId) {
        List<TDept> list = this.list(Wrappers.<TDept>lambdaQuery()
                .eq(TDept::getParentId, 0)
                .eq(TDept::getTenantId, tenantId));
//        遍历所有的
        for (TDept tDept : list) {
            TTenant tTenant = tenantMapper.selectOne(Wrappers.<TTenant>lambdaQuery()
                    .eq(tDept.getTenantId() != null, TTenant::getId, tDept.getTenantId()));
            tDept.setTenantName(tTenant.getName());
            //        根据父类id查询它线面的所有子分类数据
            List<TDept> children = this.list(Wrappers.<TDept>lambdaQuery()
                    .eq(TDept::getParentId, tDept.getDeptId()));
            for (TDept child : children) {
                TTenant tTenant1 = tenantMapper.selectOne(Wrappers.<TTenant>lambdaQuery()
                        .eq(child.getTenantId() != null, TTenant::getId, child.getTenantId()));
                tDept.setTenantName(tTenant1.getName());
            }
            tDept.setChildren(children);
        }
        return list;
    }
}




